<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="菜品分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['categoryId', validatorRules.categoryId]" placeholder="请选择菜品分类" >
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(c,index) in categoryList" :key="index" :value="c.categoryId">{{c.categoryName}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="正方形图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload :isMultiple="false" v-decorator="['cpPicture']"></j-image-upload>
              <div class="ant-form-explain">注：仅支持图片格式且文件大小500K以内，图片尺寸200X200px为最佳。</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="长方形图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload :isMultiple="false" v-decorator="['cpSquarePicture']"></j-image-upload>
              <div class="ant-form-explain">注：仅支持图片格式且文件大小500K以内，图片尺寸200X400px为最佳。</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="菜品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['cpName', validatorRules.cpName]" placeholder="请输入菜品名称"></a-input>
              <div class="ant-form-explain" v-if="izAdd">注：菜名名称，添加后不可更改，最佳长度6个字符以内。</div>
              <div class="ant-form-explain" v-if="!izAdd">注：菜名名称，编辑模式下不可更改。</div>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="cpType=='1'">
            <a-form-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol" help="单位：元">
              <a-input-number v-decorator="['cpPrice', validatorRules.cpPrice]" placeholder="请输入价格" style="width: 100%"/>
            </a-form-item>
          </a-col>

          <a-col :span="24" v-if="cpType=='0'">
            <a-form-item label="线下价格" :labelCol="labelCol" :wrapperCol="wrapperCol" help="单位：元">
              <a-input-number v-decorator="['cpPrice', validatorRules.cpPrice]" placeholder="请输入价格" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="cpType=='0'">
            <a-form-item label="线上价格" :labelCol="labelCol" :wrapperCol="wrapperCol" help="单位：元">
              <a-input-number v-decorator="['cpLinePrice']" placeholder="请输入价格" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="每天供应数量" :labelCol="labelCol" :wrapperCol="wrapperCol" help="每天供应数量，默认：无限制">
              <a-input-number v-decorator="['cpQuantity', {}]" placeholder="请输入每天供应数量，默认：无限制" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="菜品介绍" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea row="3" placeholder="请输入菜品介绍"  v-decorator="['description', {}]" />
              <div class="ant-form-explain" >注：30字符以内。</div>
            </a-form-item >
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JCheckbox from '@/components/jeecg/JCheckbox'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  export default {
    name: 'OrdCpInfoForm',
    components: {
      JFormContainer,
      JDate,
      JImageUpload,
      JCheckbox,
      JSearchSelectTag
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        izAdd: false,//是否是新增
        model: {},
        cpExtModel:{},//菜品扩展信息
        categoryList:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          status: {
            rules: [
              { required: true, message: '请选择状态!'},
            ]
          },
          cpName: {
            rules: [
              { required: true, message: '请输入菜品名称!'},
            ]
          },
          cpPrice: {
            rules: [
              { required: true, message: '请输入价格!'},
            ]
          }
        },
        url: {
          add: "/ord/ordCpInfo/add",
          edit: "/ord/ordCpInfo/edit",
          queryById: "/ord/ordCpInfo/queryById",
          categoryList: "/ord/category/cpCategoryList",
          queryExtByCpId: "/ord/ordCpInfo/queryExtByCpId"
        },
        cpType:0,
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.showCategory();
    },
    methods: {
      add (cpType) {
        this.cpType=cpType;
        var record={cpType:cpType}
        this.edit(record);

      },
      edit (record) {
        const that = this;
        this.form.resetFields();
        if(record.categoryId =='0'){
          record.categoryId = '';
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'cpType','remark','cpId','cpName','cpPicture','cpPrice','cpLinePrice','description','supplyNames','cpQuantity','cpMealMaxLimit','cpSquarePicture','categoryId'))

          if(!that.model.id){
            that.izAdd = true;
          } else {
            that.izAdd = false;
          }

          //菜品扩展信息
          //this.queryCpExt(record.cpId);
        })
      },
      //获取菜品扩展信息
      queryCpExt(id){
        if (!id){
          return;
        }
        const that = this;
        let params = {cpId:id};
        getAction(this.url.queryExtByCpId,params).then((res)=>{
          if(res.success){
            that.cpExtModel = Object.assign({}, res.result);
            console.log("cpExtModel",that.cpExtModel)
            that.$nextTick(() => {
              that.form.setFieldsValue(pick(that.cpExtModel,'recommend'))
            })
          }
        });
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },

      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let izEdit = false;
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
              izEdit = true;
            }

            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)

            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })

          }
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'remark','cpId','cpName','cpPicture','cpPrice','cpLinePrice','description','supplyNames','cpQuantity','cpMealMaxLimit','cpSquarePicture','categoryId'))
      },
      showCategory(){
        getAction(this.url.categoryList).then((res)=>{
          if(res.success){
            this.categoryList=res.result;
          }
        });
      }
    }
  }
</script>
